<?php
namespace app\serverfund\logic;
use \app\manage\logic\BaseLogic;

/**
 * @author:      李颖
 * @DateTime:    2017-09-20 14:44:44
 * @Description: 积分派发逻辑层
 */

class IntegralDistribute extends BaseLogic
{
	protected $name = 'integral_distribute';
	protected $config = 'db_config_fund';
	private $integralDistributeModel = '\app\serverfund\model\IntegralDistribute';

	/*添加资金派发积分的记录*/
	public function add($data): array 
	{
		$manageAppId = \app\manage\service\ApiBase::getAppId();  //总后台app_id
		if($manageAppId != $data['app_id'] && $data['is_public'] == 1){ return verify_err(null, '只有亚数后台才可以添加公共读取的记录'); }

		$res = $this->model($this->integralDistributeModel)->save($data);
		return auto_return($res, '成功添加派发积分记录');
	}

	/*获取某应用下的最新一条积分派发记录*/
	public function getOneNew($data): array 
	{
		$manageAppId = \app\manage\service\ApiBase::getAppId();
        $_whereOr = $manageAppId == $data['app_id'] ? true : '`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR is_public = 1';
        $_where['status'] = 1;
        $oneNew = $this->model($this->integralDistributeModel)->where($_where)->where($_whereOr)->order('id DESC')->find();
        return auto_return($oneNew, '成功获取最新一条记录');
	}

	/*后台获取积分派发记录列表*/
	public function list($data): array 
	{
		$manageAppId = \app\manage\service\ApiBase::getAppId();
        $_where = $manageAppId == $data['app_id'] ? true : '`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR is_public = 1';
        $list = $this->model($this->integralDistributeModel)
        			->where($_where)
        			->order('id DESC')
        			->paginate(null, false, ['page'=>$data['page'], 'list_rows'=>$data['listRows']])
        			->toArray();
        return auto_return_page($list);
	}


}